package com.yb.xczx.cloud.user.controller;

import com.yb.core.model.XczxResult;
import com.yb.xczx.cloud.user.bean.Oauth2TokenResult;
import com.yb.xczx.cloud.user.feign.client.AuthBasicClient;
import com.yb.xczx.cloud.user.feign.client.AuthNoAuthClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/19 15:43
 * FileName: LoginController
 * Description:
 */

//@RestController
@Slf4j
public class LoginController {

    @Autowired
    private AuthBasicClient authClient;

    @Autowired
    private AuthNoAuthClient authNoAuthClient;
    @GetMapping("login")
    public XczxResult login(){

        return authClient.login("admin","admin");
    }

    @GetMapping("/login/key")
    public XczxResult loginKey(){
        //TODO 返回随机字符串
        return XczxResult.success();
    }


    @GetMapping("/login/user")
    public XczxResult loginUser(String state){
        //TODO 查询用户是否授权成功
        return XczxResult.success();
    }
    @GetMapping("/oauth2/authorize")
    public String authorize(String accessToken,String state){
        String responseType="code";
        String clientId="login-client";
        String scope="openid%20profile";
        String redirectUri="http://passport.51xuecheng.cn:8080/login/oauth2/code/login-client";
//        response_type=code&client_id=login-client&scope=openid%20profile&redirect_uri=
        String authorize = authNoAuthClient.authorize(responseType, clientId, scope, redirectUri, accessToken,"");
        return authorize;
    }


    @GetMapping("/login/oauth2/code/login-client")
    public String loginCode(String code){
        String access_token="";
        log.info("code:{}",code);
//        POST http://localhost:9000/oauth2/token?code={{authCode}}&grant_type=authorization_code&redirect_uri=http://client:8080/login/oauth2/code/login-client&scope=openid%20profile
        String grant_type="authorization_code";
        String redirect_uri="http://passport.51xuecheng.cn:8080/login/oauth2/code/login-client";
        String scope="openid%20profile";
        String result=authClient.getToken(code,grant_type,redirect_uri,scope,access_token);
        return  XczxResult.success(result).toJSON();
    }



}
